<?php

declare(strict_types=1);

namespace App\Model;

use Hyperf\DbConnection\Model\Model;

/**
 *  用户推荐值
 */
class UserRecValue extends Model
{
    protected $table = 'user_rec_value';

    protected $fillable = [
        'user_id', 'hot_value', 'new_value', 'other_value'
    ];

    /**
     * 精选榜单
     */
    static function initHot()
    {
        log_info(__CLASS__ . "==Start-initHot==" . date('Y-m-d H:i:s'));
        $hostInfo = [];
        $initInfo = ['hot_value' => 0];

        //共用部分计算值
        self::commonCalValue('hot_value', $hostInfo, $initInfo);

        foreach ($hostInfo as $userId => $item) {
            self::addRecord($userId, $item);
        }
        log_info(__CLASS__ . "==End-initHot==" . date('Y-m-d H:i:s'));
        return $hostInfo;
    }


    /**
     * 最新榜单
     */
    static function initNew()
    {
        log_info(__CLASS__ . "==Start-initNew==" . date('Y-m-d H:i:s'));
        $hostInfo = [];
        $initInfo = ['new_value' => 0];

        //共用部分计算值
        self::commonCalValue('new_value', $hostInfo, $initInfo);

        //最近7天注册的
        $users = UserProfile::getRecentRegister(7, true);
        foreach ($users as $userId) {
            if (!isset($hostInfo[$userId])) $hostInfo[$userId] = $initInfo;
            $hostInfo[$userId]['new_value'] += 15;
        }

        foreach ($hostInfo as $userId => $item) {
            self::addRecord($userId, $item);
        }
        log_info(__CLASS__ . "==End-initNew==" . date('Y-m-d H:i:s'));
        return $hostInfo;
    }

    /**
     * 其他榜单
     */
    static function initOther()
    {
        log_info(__CLASS__ . "==Start-initOther==" . date('Y-m-d H:i:s'));
        $hostInfo = [];
        $initInfo = ['other_value' => 0];
        $filed = 'other_value';

        //资料
        $users = UserProfile::getRecProfiles(false);
        foreach ($users as $user) {
            if (!isset($hostInfo[$user['user_id']])) $hostInfo[$user['user_id']] = $initInfo;
            //声音
            if ($user['voice']) $hostInfo[$user['user_id']][$filed] += 5;
            //图片
            if ($user['poster']) {
                $posters = explode(',', $user['poster']);
                count($posters) >= 2 && $hostInfo[$user['user_id']][$filed] += 5;
            }
            //是否发布动态
            $check = Moment::checkCount($user['user_id']);
            if ($check > 0) $hostInfo[$user['user_id']][$filed] += 5;

            //在线
            if ($user['online']) $hostInfo[$user['user_id']][$filed] += 10;
        }

        //最近7天注册的
        $users = UserProfile::getRecentRegister(7, true);
        foreach ($users as $userId) {
            if (!isset($hostInfo[$userId])) $hostInfo[$userId] = $initInfo;
            $hostInfo[$userId][$filed] += 15;
        }

        foreach ($hostInfo as $userId => $item) {
            self::addRecord($userId, $item);
        }
        log_info(__CLASS__ . "==End-initOther==" . date('Y-m-d H:i:s'));
        return $hostInfo;
    }


    /**
     * 共用部分计算值
     */
    static function commonCalValue($filed = 'hot_value', &$hostInfo = [], $initInfo)
    {
        //免费接听
        $freeCalls = VoiceCall::getCallRecords(true);
        foreach ($freeCalls as $call) {
            if (!isset($hostInfo[$call['chat_user_id']])) $hostInfo[$call['chat_user_id']] = $initInfo;
            $hostInfo[$call['chat_user_id']][$filed] += 10;
        }

        //常规电话
        $calls = VoiceCall::getCallRecords(false);
        foreach ($calls as $call) {
            if (!isset($hostInfo[$call['chat_user_id']])) $hostInfo[$call['chat_user_id']] = $initInfo;
            $hostInfo[$call['chat_user_id']][$filed] += 5;
        }

        //资料
        $users = UserProfile::getRecProfiles(true);
        foreach ($users as $user) {
            if (!isset($hostInfo[$user['user_id']])) $hostInfo[$user['user_id']] = $initInfo;
            //声音
            if ($user['voice']) $hostInfo[$user['user_id']][$filed] += 5;
            //图片
            if ($user['poster']) {
                $posters = explode(',', $user['poster']);
                count($posters) >= 3 && $hostInfo[$user['user_id']][$filed] += 5;
            }
            //在线
            if ($user['online']) $hostInfo[$user['user_id']][$filed] += 10;
        }
    }

    /**
     * 插入记录
     */
    static function addRecord($userId, $info)
    {
        return self::updateOrCreate([
            'user_id' => $userId,
        ], $info);
    }

    /**
     * 精选榜用户
     */
    static function getHotUsers()
    {
        return self::where([['hot_value', '>', 0]])->orderBy('hot_value', 'desc')->pluck('user_id')->toArray();
    }

    /**
     * 最新榜用户
     */
    static function getNewUsers()
    {
        return self::where([['new_value', '>', 0]])->orderBy('new_value', 'desc')->pluck('user_id')->toArray();
    }

    /**
     * 其他女用户
     */
    static function getOtherUsers()
    {
        return self::where([['other_value', '>', 0]])->orderBy('other_value', 'desc')->pluck('user_id')->toArray();
    }
}
